import {Encrypter} from "../Encrypters/Encrypter";
import {GameDataManager} from "./GameDataManager";
import {GameDataKey} from "../Enums/GameDataKey";
import {WebClient} from "../Network/Web/WebClient";
import {StoreManager} from "./StoreManager";
import {FFunction} from "../Support/FFunction";

export class WebManager {
    private static _instance: WebManager = null;
    public static get instance(): WebManager {
        if (!this._instance) this._instance = new WebManager();
        return this._instance;
    }

    private webURL: string = "";
    private API: string = "";//"GameApi/";// "ExternalGameAPI/GameApi/"
    private version: string = "2018.6.13.1";
    private encrypter: Encrypter;
    private clientDict: { [key: string]: object };

    public constructor() {
        this.clientDict = {};
        this.encrypter = new Encrypter();
        var webData: any = GameDataManager.instance.getDictData(GameDataKey.WEB_DATA);
        this.webURL = webData.ApiUrl;//+API;
        if (this.webURL.charAt(this.webURL.length - 1) == "/") this.webURL += this.API;
        else this.webURL += this.API;
        // else this.webURL+="/"+this.API;
    }

    public static GetWebData(webApi: string, data: any, onComplete: FFunction, onFail: FFunction = null): void {
        if (this.instance.clientDict[webApi] != undefined) return;// 不允许重复调用接口
        this.instance.clientDict[webApi] = {onComplete: onComplete, onFail: onFail};
        var client: WebClient = this.instance.client;
        client.setRequestHeader("Content-Type", "application/json");
        // var webData:any = GameDataManager.instance.getDictData(GameDataKey.WEB_DATA);
        // let serviceURL:string = "";
        // if(webApi==WebAPI.GetToken)
        // {
        //     serviceURL = webData.ApiUrl;
        //     client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded,application/json");
        //     client.setRequestHeader("serviceToken","1AF7E73A90369FEC77544F5119C5B4F34C68525154AB5951F5F46A3576299B90");
        // }else
        // {
        //     serviceURL = webData.ServiceApiUrl;
        //     client.setRequestHeader("Content-Type","application/json");
        //     client.setRequestHeader("Tick",GameDataManager.instance.player.AuthToken);
        // }
        client.open(this.instance.webURL + webApi);
        client.clientName = webApi;
        client.send(JSON.stringify(data));
        // 先屏蔽掉数据加密
        //client.send(this.instance.encrypter.EncodeWebData(JSON.stringify(data)));
    }

    private onWebClientGetData(client: WebClient, data: any): void {
        // 先屏蔽掉数据加密
        //data = this.encrypter.DecodeWebData(data);
        data = JSON.parse(data);
        if (this.clientDict[client.clientName] != undefined) {
            let o: any = this.clientDict[client.clientName];
            if (o.onComplete != null) o.onComplete.excute([data]);//.//call(o.obj,data);
        }
        this.storeClient(client);
    }

    private onWebClientFail(client: WebClient): void {
        console.log("[WebManager] 无法获取Web接口===>", client.clientName, " 的数据!!!");
        if (this.clientDict[client.clientName] != undefined) {
            let o: any = this.clientDict[client.clientName];
            if (o.onFail != null) o.onFail.excute();//.call(o.obj);
        }
        this.storeClient(client);
    }

    private onWebClientTimeOut(client: WebClient): void {
        console.log("[WebManager]调用Web接口===>", client.clientName, " 超时!!!");
        if (this.clientDict[client.clientName] != undefined) {
            let o: any = this.clientDict[client.clientName];
            if (o.onFail != null) o.onFail.excute();//.call(o.obj);
        }
        this.storeClient(client);
    }

    private storeClient(client: WebClient): void {
        if (this.clientDict[client.clientName] != undefined)
            delete this.clientDict[client.clientName];
        client.dispose();
    }

    private get client(): WebClient {
        var client: WebClient = StoreManager.New(WebClient);
        client.addCallBacks(this, this.onWebClientGetData, null, this.onWebClientFail);
        return client;
    }
}